package servlet;

import dao.GoodsDao;
import pojo.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author konley
 * @date 2020-06-20 14:40
 */
@MultipartConfig
@WebServlet("/GoodsServlet")
public class GoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m = request.getParameter("m");
        if(m.equals("add")) {    //执行添加操作
            //javax.servlet.http.Part:servlet 3.0提供的实现文件上传的类
            //multipart/form-data的post请求会封装成part的对象
            Part part = request.getPart("url");
            //获取文件名
            String filename = getFileName(part);
            //再加工 处理成当前毫秒数.jpg 比如 ./photo/goods.51234123.jpg
            String photo;
            if(filename !=null ){
                long currentTimeMillis = System.currentTimeMillis();
                photo = "./photo/goods" + currentTimeMillis + filename.substring(filename.lastIndexOf("."));
                filename = getServletContext().getRealPath("/") + "/" + photo+"/";
                part.write(filename);
            } else {
                photo = "./photo/goods/default.jpg";    //找不到就用default
            }
            String name = request.getParameter("name");
            String type = request.getParameter("type");
            String money = request.getParameter("money");
            String info = request.getParameter("info");
            String stock = request.getParameter("stock");
            String url = photo;
            Goods goods = new Goods(
              1,name,type,money,info,stock,url
            );
            System.out.println(goods);
            boolean b = new GoodsDao().add(goods);
            System.out.println(b);
            //根据名字查 跳转到详情页
            goods = new GoodsDao().getGoodsByName(name);
            int id = goods.getId();
            System.out.println(id);
            request.getRequestDispatcher("ShowGoods?id="+id).forward(request,response);
        }else if(m.equals("update")){  //执行修改操作
            int id = Integer.parseInt(request.getParameter("id"));
            Goods goods = new GoodsDao().getGoodsById(id);
            request.setAttribute("goods",goods);
            request.getRequestDispatcher("/updateGoods.jsp").forward(request,response);
        }else if(m.equals("toUpdate")){
            int id = Integer.parseInt(request.getParameter("id"));
            Goods goods = new Goods(
                    id,
                    request.getParameter("name"),
                    request.getParameter("type"),
                    request.getParameter("money"),
                    request.getParameter("info"),
                    request.getParameter("stock"),
                    ""
            );
            boolean b = new GoodsDao().update(goods);
            System.out.println("update = "+b);
            request.getRequestDispatcher("ShowGoods?id="+Integer.parseInt(request.getParameter("id"))).forward(request,response);
        }else if(m.equals("delete")){
            int id = Integer.parseInt(request.getParameter("id"));
            boolean b = new GoodsDao().delete(id);
            String comm = "";
            List<Goods> list = new GoodsDao().getListByName(comm);
            request.setAttribute("list", list);
            request.getRequestDispatcher("/managerView.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    private String getFileName(Part part) {
        // 获取 header 信息中的 content-disposition，如果为文件，则可以从其中提取出文件名
        String cotentDesc = part.getHeader("content-disposition");
        String fileName = null;
        Pattern pattern = Pattern.compile("filename=\".+\"");
        Matcher matcher = pattern.matcher(cotentDesc);
        if (matcher.find()) {
            fileName = matcher.group();
            fileName = fileName.substring(10, fileName.length() - 1);
        }
        return fileName;
    }
}
